﻿package
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class Magic_UI extends MovieClip
	{
		public var game_handler:GameHandler,make_enemy_button,set_variables_button,distance_scaler;
		public var spitting_slime_spawn_count,green_slime_spawn_count,yellow_slime_spawn_count,red_slime_spawn_count;
		public var mana_amount_text:TextField,create_mage_button:MovieClip;
		public var new_mage_cost = 500;
		public function Magic_UI(gH:GameHandler)
		{
			game_handler = gH;
			create_mage_button.addEventListener(MouseEvent.CLICK,make_new_mage);
			make_enemy_button.addEventListener(MouseEvent.CLICK,make_enemies);
			set_variables_button.addEventListener(MouseEvent.CLICK,set_debug_variables);
		}
		public function run(){
			mana_amount_text.text = Math.floor(game_handler.mana_amount).toString();
		}
		public function set_debug_variables(evt){
			game_handler.resize_effect = distance_scaler.text;
		}
		public function make_new_mage(evt){
			if(game_handler.mana_amount > new_mage_cost){
				game_handler.mana_amount -= new_mage_cost;
				game_handler.generate_player_unit();
			}
		}
		public function make_enemies(evt){
			var i = 0;
			var p;
			for(i = 0; i < green_slime_spawn_count.text; i ++){
				p = make_random_screen_point();
				game_handler.generate_enemy("green_slime",p.x,p.y);
			}
			for(i = 0; i < red_slime_spawn_count.text; i ++){
				p = make_random_screen_point();
				game_handler.generate_enemy("red_slime",p.x,p.y);
			}
			for(i = 0; i < yellow_slime_spawn_count.text; i ++){
				p = make_random_screen_point();
				game_handler.generate_enemy("yellow_slime",p.x,p.y);
			}
			for(i = 0; i < spitting_slime_spawn_count.text; i ++){
				p = make_random_screen_point();
				game_handler.generate_enemy("spitting_slime",p.x,p.y);
			}
			
		}
		public function make_random_screen_point(){
			var side = Math.floor(Math.random()*4);
			var tarx = 0,tary=0;
			if(side == 0){
				tarx = Math.random()*game_handler.STAGE_WIDTH; tary = 0;
			}else if (side == 1){
				tarx = game_handler.STAGE_WIDTH; tary = Math.random()*game_handler.STAGE_HEIGHT;
			}
			else if (side == 2){
				tarx = Math.random()*game_handler.STAGE_WIDTH; tary = game_handler.STAGE_HEIGHT;
			}else if (side == 3){
				tarx = 0; tary = Math.random()*game_handler.STAGE_HEIGHT;
			}
			return new Point(tarx,tary);
		}
	}
}